import { ResManager } from "./ResManager";
import { DataControl } from "./DataControl";
const {ccclass, property} = cc._decorator;

@ccclass
export class ScoreItem extends cc.Component {
    //当前放置水果的名称
    private _nick_name : string = "";
    public set NickName(name : string) {
        this._nick_name = name;
    }
    public get NickName() : string {
        return this._nick_name;
    }
    
    //当前放置水果的输赢分数
    private _score : number = 0;
    public set Score(score : number) {
        this._score = score;
    }
    public get Score() : number {
        return this._score;
    }
    //当前放置水果的消耗分数
    private _cost : number = 0;
    public set Cost(cost : number) {
        this._cost = cost;
    }
    public get Cost() : number {
        return this._cost;
    }
    //当前放置水果的选择数量
    _num : number = 0;
    public set Num(num : number) {
        this._num = num;
    }
    public get Num() : number {
        return this._num;
    }

    //当前放置水果的ICON
    @property(cc.Sprite)
    fruit_sprite : cc.Sprite = null;
    //选择水果的数量
    @property(cc.Label)
    num_label : cc.Label = null;

    hall_node: cc.Node = null;

    loadInit(fruit_icon : string){
        let self = this;
        this.num_label.string = this.Num.toString();

        ResManager.getInstance().getSpriteFrameByPath(fruit_icon, (_sprite_frame)=>{
            self.fruit_sprite.spriteFrame = _sprite_frame;
        });

        this.hall_node = this.node.parent.parent;
    }

    //加号按钮点击事件
    addBtnClickEvent(event_touch) {
        cc.log("add click event");
        let controlInst = DataControl.getInstance();
        controlInst.CostSum += this.Cost;
        if(controlInst.judgeConstAndScore()){
            this.hall_node.getComponent("hall").showAlertTips("您的分数不足");
            controlInst.CostSum -= this.Cost;
            return;
        }
        this.Num++;
        this.num_label.string = this.Num.toString();
    }
    //减号按钮点击事件
    minusBtnClickEvent(event_touch) {
        cc.log("minus click event");
        if(this.Num === 0) return;
        this.Num--;
        this.num_label.string = this.Num.toString();
        
        let controlInst = DataControl.getInstance();
        controlInst.CostSum -= this.Cost;
    }
}
